#include <uMP3.h>

uMP3 ump3(Serial1);

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  
  ump3.sync();
  ump3.stop();
}

void loop()
{
  char c, status = 'S';
  uint8_t i, volume = 20;
  uint16_t ptime = 0;

  volume = ump3.getvolume();  // this only gets the right volume

  while(1)
  {
    while(!Serial.available())
    {
      // get information and display
      switch((status = ump3.getplaybackstatus()))
      {
        case 'P':
          Serial.print("Playing ");
          break;
        case 'D':
          Serial.print("Paused ");
          break;
        case 'S':
          Serial.print("Stopped ");
          break;
      }
      
      if(status != 'S')
      {
        ptime = ump3.getplaybackinfo(uMP3::PLAYTIME);
        Serial.print(ptime/60,DEC); Serial.print(':');
        if(ptime%60 < 10) Serial.print('0');
        Serial.print(ptime%60,DEC);
        Serial.print(' ');
        Serial.print(ump3.getplaybackinfo(uMP3::BITRATE), DEC); Serial.print(" kbps ");
        Serial.print(ump3.getplaybackinfo(uMP3::SAMPLERATE), DEC); Serial.print(" KHz ");
      }
      
      Serial.print("vol: "); Serial.print(volume, DEC);
      for(i = 0; i < 28; i++) Serial.print(' ');
      Serial.print('\r');
    }
    
    c = Serial.read();
    
    switch(c)
    {
      case 'p':
        // pause
        if(status == 'D')
        {
          // fade in
          ump3.playpause();
          ump3.fade(volume);
        }
        else if(status == 'P')
        {
          // fade out
          ump3.fade(254);
          ump3.playpause();
        }
        break;
      case 's':
        ump3.stop();
        break;
      case 'f':
        ump3.playfile("b0000.mp3");
        break;
      case 'k':
        if(volume < 254) volume++;
        if(status != 'D') ump3.setvolume(volume);
        break;
      case 'i':
        if(volume > 0) volume--;
        if(status != 'D') ump3.setvolume(volume);
        break;
    }
  }
}
